package com.ruoyi.web.controller.quantization;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.quantization.domain.SysMorality;
import com.ruoyi.quantization.service.ISysMoralityService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.quantization.domain.SysMoralityGl;
import com.ruoyi.quantization.service.ISysMoralityGlService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 品德（详情）Controller
 * 
 * @author ruoyi
 * @date 2024-10-02
 */
@RestController
@RequestMapping("/system/gl")
public class SysMoralityGlController extends BaseController
{
    @Autowired
    private ISysMoralityGlService sysMoralityGlService;

    @Autowired
    private ISysMoralityService sysMoralityService;

    /**
     * 查询品德（详情）列表
     */
    @PreAuthorize("@ss.hasPermi('system:gl:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysMoralityGl sysMoralityGl)
    {
        startPage();
        List<SysMoralityGl> list = sysMoralityGlService.selectSysMoralityGlList(sysMoralityGl);
        return getDataTable(list);
    }

    /**
     * 导出品德（详情）列表
     */
    @PreAuthorize("@ss.hasPermi('system:gl:export')")
    @Log(title = "品德（详情）", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysMoralityGl sysMoralityGl)
    {
        List<SysMoralityGl> list = sysMoralityGlService.selectSysMoralityGlList(sysMoralityGl);
        ExcelUtil<SysMoralityGl> util = new ExcelUtil<SysMoralityGl>(SysMoralityGl.class);
        util.exportExcel(response, list, "品德（详情）数据");
    }

    /**
     * 获取品德（详情）详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:gl:query')")
    @GetMapping(value = "/{moralityId}")
    public AjaxResult getInfo(@PathVariable("moralityId") Long moralityId)
    {
        return success(sysMoralityGlService.selectSysMoralityGlByMoralityId(moralityId));
    }

    /**
     * 新增品德（详情）
     */
    @PreAuthorize("@ss.hasPermi('system:gl:add')")
    @Log(title = "品德（详情）", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysMoralityGl sysMoralityGl)
    {
        // 查询到该同学
        // 添加待审核分数
        // 添加待审核项目数量
        SysMorality sysMorality=sysMoralityService.selectSysMoralityByMoralityStdId(sysMoralityGl.getMoralityStdId());

        // 项目数量+1
        BigDecimal amount = new BigDecimal(sysMorality.getMoralityWord());
        amount = amount.add(new BigDecimal(1));
        sysMorality.setMoralityWord(amount.toString());

        // 获取奖励分和扣除分
        // BigDecimal stdaward = new BigDecimal(sysMorality.getMoralityStdAward());
        // BigDecimal stddeduction = new BigDecimal(sysMorality.getMoralityStdDeduction());

        // 获得待审核分数
        BigDecimal award = new BigDecimal(sysMorality.getMoralityAward());


        // 获得传递数据的分数
        BigDecimal mora_await = new BigDecimal(sysMoralityGl.getMoralityAward());

        // 判断是加分还是减分 0 加分 1减分
        String type = sysMoralityGl.getMoralityType();
        if(type.equals("0")){
            //加分
            award = award.add(mora_await);
        }else{
            //减分
            award = award.subtract(mora_await);
        }
        // 添加到数据
        sysMorality.setMoralityAward(award.toString());

        // 保存数据
        sysMoralityService.updateSysMorality(sysMorality);

        return toAjax(sysMoralityGlService.insertSysMoralityGl(sysMoralityGl));
    }

    /**
     * 修改品德（详情）
     */
    @PreAuthorize("@ss.hasPermi('system:gl:edit')")
    @Log(title = "品德（详情）", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysMoralityGl sysMoralityGl)
    {
        // 修改品德 先从之前存储过的删掉再修改成新的
        // 所以要先查询之前的

        // 获得该同学的待审核分数（外表）
        SysMorality sysMorality=sysMoralityService.selectSysMoralityByMoralityStdId(sysMoralityGl.getMoralityStdId());
        BigDecimal award = new BigDecimal(sysMorality.getMoralityAward());




        // 获取之前的数据
        SysMoralityGl oldMora=sysMoralityGlService.selectSysMoralityGlByMoralityId(sysMoralityGl.getMoralityId());
        // 获得之前的分数
        BigDecimal mora_await = new BigDecimal(oldMora.getMoralityAward());
        // 判断之前是加分还是减分（弥补回来）
        String type = oldMora.getMoralityType();
        if(type.equals("0")){
            //加分
            award = award.subtract(mora_await);
        }else{
            //减分
            award = award.add(mora_await);
        }

        // 获取当前传递的分数
        mora_await = new BigDecimal(sysMoralityGl.getMoralityAward());
        // 判断当前是加分还是减分
        type = sysMoralityGl.getMoralityType();
        if(type.equals("0")){
            //加分
            award = award.add(mora_await);
        }else{
            //减分
            award = award.subtract(mora_await);
        }

        // 添加到数据
        sysMorality.setMoralityAward(award.toString());

        // 保存数据
        sysMoralityService.updateSysMorality(sysMorality);


        return toAjax(sysMoralityGlService.updateSysMoralityGl(sysMoralityGl));
    }






    /**
     * 删除品德（详情）
     */
    @PreAuthorize("@ss.hasPermi('system:gl:remove')")
    @Log(title = "品德（详情）", businessType = BusinessType.DELETE)
	@DeleteMapping("/{moralityIds}")
    public AjaxResult remove(@PathVariable Long[] moralityIds)
    {
        SysMoralityGl sysMoralityGl = sysMoralityGlService.selectSysMoralityGlByMoralityId(moralityIds[0]);
        SysMorality sysMorality=sysMoralityService.selectSysMoralityByMoralityStdId(sysMoralityGl.getMoralityStdId());
        BigDecimal amount = new BigDecimal(sysMorality.getMoralityWord());

        // 获得待审核分数
        BigDecimal award = new BigDecimal(sysMorality.getMoralityAward());

        for(int i=0;i<moralityIds.length;i++){
            // 获取删除的每一条数据
            Long ids = moralityIds[i];
            sysMoralityGl = sysMoralityGlService.selectSysMoralityGlByMoralityId(ids);
            // 获取当前审核状态
            String await = sysMoralityGl.getMoralityAwait();
            if(await.equals("1")){
                // 不可删除 (审核成功)的
                return success("删除失败");
            }else if(await.equals("0")){
                // 待审核的
                // 先把原先的数量减1 并 恢复分数
                amount=amount.subtract(new BigDecimal(1));
                // 判断是加分还是减分 0 加分 1减分
                String type = sysMoralityGl.getMoralityType();
                // 获得传递数据的分数
                BigDecimal mora_await = new BigDecimal(sysMoralityGl.getMoralityAward());
                if(type.equals("0")){
                    //加分
                    award = award.subtract(mora_await);
                }else{
                    //减分
                    award = award.add(mora_await);
                }
            }else{
                // 审核失败的
//                amount=amount.subtract(new BigDecimal(1));
            }
            // 添加到数据
            sysMorality.setMoralityAward(award.toString());
            sysMorality.setMoralityWord(amount.toString());
            // 保存数据
            sysMoralityService.updateSysMorality(sysMorality);
        }
        return toAjax(sysMoralityGlService.deleteSysMoralityGlByMoralityIds(moralityIds));
    }



    /**
     *审核品德（详情）
     */
    @PreAuthorize("@ss.hasPermi('system:gl:check')")
    @Log(title = "品德（详情）", businessType = BusinessType.INSERT)
    @PostMapping("/check")
    public AjaxResult check(@RequestBody SysMoralityGl sysMoralityGl){
        // 核对品德，首先判断最终是否决策
        // 如果最终无决策则可以决策

        // 获取当前登录的用户是否是教师/班长

        // 获取当前的用户信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        System.out.println(loginUser);


        return toAjax(sysMoralityGlService.updateSysMoralityGl(sysMoralityGl));
    }
}
